<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class roleController extends Controller
{
    public function index()
    {
        //查询数据
        $result = DB::table('role')->get();
        //跳转页面
        return view('admin/role', ['result' => $result]);
    }

    public function add()
    {
        //查询数据(权限)
        $result = DB::table('permiss')->get();

        //跳转到添加页面
        return view('admin/roleadd', ['result' => $result]);
    }

    //添加数据方法
    public function insert(Request $request)
    {
        $data = array(
            'or_name' => $request->or_name, //角色
            'start' => $request->start,    //状态
        );
        $id = $request->per_name;    //权限

        //插入角色表（role）返回ID
        $result = DB::table('role')->insertGetId($data);

        //把id插入表中（limt）权限id插入
        foreach ($id as $v) {
            //把id插入表中（limt）
            $li_id = DB::insert("insert into `limt` (`li_id`,`li_pid`) VALUES ($result,$v)");
        }
        if ($li_id) {
            return redirect('/prompt')->with(['message'=>'添加角色成功！','url' =>'/role', 'jumpTime'=>3,'status'=>true]);
        }
    }


    //改变start状态
    public function start($id, $start)
    {
        //接收数据
        $id = $id;
        $start = $start;

        //修改状态
        if ($start == 0) {
            $start = 1;
        } else {
            $start = 0;
        }

        //修改数据库
        $result = DB::table('role')
            ->where('id', $id)
            ->update([
                'start' => $start,
            ]);

        return $result;

    }

    //删除数据ajax请求
    public function del($id)
    {
        //接收数据
        $id = $id;
        $result = DB::delete('delete from `role` where id=' . $id);

        if ($result) {
            echo 1;
        } else {
            echo 2;
        }
    }

    //编辑
    public function update($id)
    {
        //获取数据ID
        $id = $id;
        //查询数据送到前台
        $result = DB::select("select r.`or_name`,r.`start`,l.`li_pid`,p.`per_name` 
                  from `role` r,`limt` l,`permiss` p
                  where r.`id` = l.`li_id` and r.`id` = $id and l.`li_pid` = p.`id`");
        $or_name = $result[0]->or_name;
        $start = $result[0]->start;
//        dd($result);
        foreach ($result as $v) {
            $li[0][$v->li_pid] = $v->per_name;
        }
        $data[0]['or_name'] = $or_name;
        $data[0]['start'] = $start;
        $data[0]['id'] = $id;

        //查询所有的权限信息
        $permiss = DB::table('permiss')->get();
//        dd($data);
        //判断跳转
        if ($data) {
            return view('admin/roleupdate', ['data' => $data, 'permiss' => $permiss, 'li' => $li]);
        }

    }


    //编辑方法
    public function upd(Request $request)
    {
        //接收数据
            $or_name = $request->or_name;      //角色
            $start   = $request->start;        //状态
            $id      = $request->per_name;     //权限ID
            $rid     = $request->id;           //角色ID

        //修改角色表的数据(role)表
        $result = DB::update("update `role` set `or_name`= '$or_name',`start`= '$start' WHERE id=".$rid);
        //删除数据库中原有的li_id和li_pid
        $del = DB::delete("delete from `limt` WHERE li_id=".$rid);
        //重新插入新的权限ID和角色ID
        foreach ($id as $v) {
            //把id插入表中（limt）
            $li_id = DB::insert("insert into `limt` (`li_id`,`li_pid`) VALUES ($rid,$v)");
        }
        if ($li_id) {
            return redirect('/prompt')->with(['message'=>'编辑角色成功！','url' =>'/role', 'jumpTime'=>3,'status'=>true]);
        }
    }

    //查看角色权限
    public function look($id)
    {
        //获取数据ID
        $id = $id;
        //查询数据送到前台
        $result = DB::select("select r.`or_name`,r.`start`,l.`li_pid`,p.`per_name` 
                  from `role` r,`limt` l,`permiss` p
                  where r.`id` = l.`li_id` and r.`id` = $id and l.`li_pid` = p.`id`");
        $or_name = $result[0]->or_name;
        $start = $result[0]->start;
//        dd($result);
        foreach ($result as $v) {
            $li[0][$v->li_pid] = $v->per_name;
        }
        $data[0]['or_name'] = $or_name;
        $data[0]['start'] = $start;
        $data[0]['id'] = $id;

        //查询所有的权限信息
        $permiss = DB::table('permiss')->get();
//        dd($data);
        //判断跳转
        if ($data) {
            return view('admin/rolelook', ['data' => $data, 'permiss' => $permiss, 'li' => $li]);
        }
    }

}
